/*
 * Copyright (c) gitee.com/jiandong001.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import { MessageQueueThreadSpec , ThreadType} from './MessageQueueThreadSpec';
/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */



/**
 * Spec for creating a ReactQueueConfiguration. This exists so that CatalystInstance is able to set
 * Exception handlers on the MessageQueueThreads it uses and it would not be super clean if the
 * threads were configured, then passed to CatalystInstance where they are configured more. These
 * specs allows the Threads to be created fully configured.
 */
export class ReactQueueConfigurationSpec {

//  private mNativeModulesQueueThreadSpec: MessageQueueThreadSpec;
//  private mJSQueueThreadSpec: MessageQueueThreadSpec;
  private static builderInstance: Builder = null

  public constructor(
      private mNativeModulesQueueThreadSpec: MessageQueueThreadSpec,
      private mJsQueueThreadSpec: MessageQueueThreadSpec) {
//    this.mNativeModulesQueueThreadSpec = nativeModulesQueueThreadSpec;
//    this.mJSQueueThreadSpec = jsQueueThreadSpec;
  }

  public getNativeModulesQueueThreadSpec(): MessageQueueThreadSpec {
    return this.mNativeModulesQueueThreadSpec;
  }

  public getJSQueueThreadSpec(): MessageQueueThreadSpec {
    return this.mJsQueueThreadSpec;
  }

  public static builder(): Builder {
    return this.builderInstance?? new Builder();
  }

  public static createDefault(): ReactQueueConfigurationSpec {
//    let spec: MessageQueueThreadSpec = MessageQueueThreadSpec.newBackgroundThreadSpec("native_modules");
    return ReactQueueConfigurationSpec.builder()
        .setJSQueueThreadSpec(MessageQueueThreadSpec.newBackgroundThreadSpec("js", "../../worker/JSQueueThread.ts"))
//        .setNativeModulesQueueThreadSpec(MessageQueueThreadSpec.newBackgroundThreadSpec("native_modules", "../../worker/NativeModulesQueueThread.ts")))
        .build();
  }
}

class Builder {

  private mNativeModulesQueueSpec: MessageQueueThreadSpec;
  private mJSQueueSpec: MessageQueueThreadSpec;

  public  setNativeModulesQueueThreadSpec(spec: MessageQueueThreadSpec): Builder {
//    Assertions.assertCondition(
//        mNativeModulesQueueSpec == null, "Setting native modules queue spec multiple times!");
    if (this.mNativeModulesQueueSpec != null) throw Error("Setting native modules queue spec multiple times!")
    this.mNativeModulesQueueSpec = spec;
    return this;
  }

  public setJSQueueThreadSpec(spec: MessageQueueThreadSpec): Builder {
//    Assertions.assertCondition(this.mJSQueueSpec == null, "Setting JS queue multiple times!");
    if (this.mJSQueueSpec != null) throw Error("Setting JS queue multiple times!")
    this.mJSQueueSpec = spec;
    return this;
  }

  public build(): ReactQueueConfigurationSpec {
    //todo: both specs could not be null
    if (!this.mNativeModulesQueueSpec && ! this.mJSQueueSpec) throw Error("must be not null")
    return new ReactQueueConfigurationSpec(this.mNativeModulesQueueSpec,this.mJSQueueSpec);
  }
}
